﻿using PLCS.Application.Contracts.HisTaskApp.Dtos;
using PLCS.Application.Contracts.TaskApp.Dtos;
using PLCS.Client.IService.OrderTask;

using System.Collections.ObjectModel;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.OrderTask
{
    public class InstructionService : BaseService<InstructionDto, InstructionEditDto>, IInstructionService
    {
        public async Task AgainIssuedAsync(Guid Id)
        {
            RequestParam requestParam = new(method: Method.Post, route: $"instruction/againIssued/{Id}");
            var apiRes = await restClient.ExecuteAsync(requestParam);
            if (!apiRes.Status)
                throw new Exception(apiRes.Message);
        }

        public async Task FinshTaskAsync(Guid Id)
        {
            RequestParam requestParam = new(method: Method.Post, route: $"instruction/finshTask/{Id}");
            var apiRes = await restClient.ExecuteAsync(requestParam);
            if (!apiRes.Status)
                throw new Exception(apiRes.Message);
        }

        public override async Task<IEnumerable<InstructionDto>> GetListAsync()
        {
            RequestParam requestParam = new(method: Method.Get, route: $"instruction");
            var apiRes = await restClient.ExecuteAsync<PagedResultDto<InstructionDto>>(requestParam);
            if (apiRes.Status)
                return apiRes.Data.Items;
            else
                return Enumerable.Empty<InstructionDto>();
        }

        public override async Task UpdateAsync(InstructionEditDto input)
        {
            RequestParam requestParam = new(method: Method.Put, route: $"instruction/{input.Id}", parameter: input);
            var apiRes = await restClient.ExecuteAsync(requestParam);
            if (!apiRes.Status)
                throw new Exception(apiRes.Message);
        }

        public async Task<PagedResultDto<InstructionDto>> GetListBySearchAsync(int skipCount, int maxResultCount, string? instructionNo, string? equipmentNo, string? status)
        {
            var route = $"Instruction/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
            if (!string.IsNullOrEmpty(instructionNo))
                route += $"&instructionNo={instructionNo}";
            if (!string.IsNullOrEmpty(equipmentNo))
                route += $"&equipmentNo={equipmentNo}";
            if (!string.IsNullOrEmpty(status))
                route += $"&status={status}";
            RequestParam requestParam = new(method: Method.Get, route: route);
            var apiRes = await restClient.ExecuteAsync<PagedResultDto<InstructionDto>>(requestParam);
            if (apiRes.Status)
                return apiRes.Data;
            else
                return new PagedResultDto<InstructionDto>();
        }
    }
}